/* 
 * File:   Character.cpp
 * Author: Zeeno
 * 
 * Created on 16. březen 2012, 8:54
 */

#include "Character.h"

#include "../Math/math.h"

using namespace vertigo::math;

namespace vertigo {

    const size8 Character::size[256] = {
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //1 - 15
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //16 - 31
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //32 - 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //48
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //64
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //81
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //96
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //112 - 127
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //128
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //144
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //160
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //176
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, //192
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, //208
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, //224
        4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 240 - 255
    };

    size8 Character::sizeOf(Character ch) {
        Uint8 c = ch._value[0];
        if (inRange(c, 0, 127)) return 1;
        if (inRange(c, 192, 223)) return 2;
        if (inRange(c, 224, 239)) return 3;
        if (inRange(c, 240, 247)) return 4;
        if (inRange(c, 248, 251)) return 5;
        if (inRange(c, 252, 253)) return 6;
        return 0;
    }

    /*
    static bool Character::isValid(Character ch) {
        uchar c = ch[0];
        switch (c) {
            case (192 ... 193): return false;
            case (245 ... 255): return false;
        }

        //one octet
        if (ch[0] < 0x80) return true;

        //two octets
        if (inRange(ch[0], 0xC2, 0xDF) && inRange(ch[1], 0x80, 0xBF)) return true;

        //three octets
        if (!inRange(ch[2], 0x80, 0xBF)) return false; //in three and four octets long character is 3.octet always in range of 0x80 and 0xBF
        if (ch[0] == 0xE0 && inRange(ch[1], 0xA0, 0xBF)) return true;
        if (inRange(ch[0], 0xE1, 0xEC) && inRange[ch[1], 0x80, 0xBF]) return true;
        if (ch[0] == 0xED && inRange(ch[1], 0x80, 0x9F)) return true;
        if (inRange(ch[0], 0xEE, 0xEF) && inRange[ch[1], 0x80, 0xBF]) return true;

        //four octets
        if (!inRange(ch[3], 0x80, 0xBF)) return false; //in four octets long character is 4.octet always in range of 0x80 and 0xBF
        if (ch[0] == 0xF0 && inRange(ch[1], 0x90, 0xBF)) return true;
        if (inRange(ch[0], 0xF1, 0xF3) && inRange(ch[1], 0x80, 0xBF)) return true;
        if (ch[0] == 0xF4 && inRange(ch[1], 0x80, 0x8F)) return true;

        return false;
    }*/
}
